package edu.cornell.cs4740.qa.pos;

import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.List;

import com.aliasi.hmm.HiddenMarkovModel;
import com.aliasi.hmm.HmmDecoder;
import com.aliasi.tag.Tagging;

import edu.cornell.cs4740.qa.WordContainer;

public class PosTagger {

	HiddenMarkovModel hiddenMarkovModel;
	HmmDecoder hmmDecoder;

	public PosTagger(File hmmFile) {
		try {
			FileInputStream hmmStream = new FileInputStream(hmmFile);
			ObjectInputStream hmmObjStream = new ObjectInputStream(hmmStream);
			hiddenMarkovModel = (HiddenMarkovModel) hmmObjStream.readObject();
			hmmObjStream.close();
			hmmStream.close();
			
			hmmDecoder = new HmmDecoder(hiddenMarkovModel);
		} catch (Exception e) {
			System.err.println("Failed to create Hidden Markov Model for POS Tagging!");
			e.printStackTrace();
		}
	}

	
	// enriches the words with pos info in the word container
	public void tagTokens(List<WordContainer> tokens) {
		List<String> tokensToTag = new ArrayList<String>();
		for(WordContainer wc : tokens) {
			tokensToTag.add(wc.getWord());
		}
		
		Tagging<String> taggedWords = hmmDecoder.tag(tokensToTag);
		
		for(int x = 0; x < taggedWords.size(); x++) {
			tokens.get(x).setPos(taggedWords.tag(x));
		}
	}

}
